#include "widget.h"

#include <QDebug>
#include <QSerialPort>
#include <QSerialPortInfo>
#include "databuffer.h"
#include "serialthread.h"
#include "ui_widget.h"

Widget::Widget( QWidget *parent ) : QWidget( parent ), ui( new Ui::Widget )
{
    ui->setupUi( this );
    QMap<QString, QString> dev_map = {
        { "/tmp/ttyUSB0", "/tmp/tty_usb0" }, { "/tmp/ttyUSB1", "/tmp/tty_usb1" }, { "/tmp/ttyUSB2", "/tmp/tty_usb2" },
        { "/tmp/ttyUSB3", "/tmp/tty_usb3" }, { "/tmp/ttyUSB4", "/tmp/tty_usb4" }, { "/tmp/ttyUSB5", "/tmp/tty_usb5" },
    };

    //    QStringList serialList = { "/tmp/tty_usb0", "/tmp/tty_usb1", "/tmp/tty_usb2", "/tmp/tty_rs0", "/tmp/tty_rs1", "/tmp/tty_rs2" };

    _virtual_serial = new VirtualSerial( dev_map );

    ui->tableWidget->setColumnWidth( 0, 100 );
    ui->tableWidget->setColumnWidth( 1, 250 );
    ui->tableWidget->setColumnWidth( 2, this->width() - 350 );
    const int rowCount = 20;
    ui->tableWidget->setRowCount( rowCount );

    const auto buffer = DataBuffer::instance().getDataBuffer();
    if ( buffer.isEmpty() ) {
        for ( int i = 0; i < rowCount; i++ ) {
            QTableWidgetItem *item5_check = new QTableWidgetItem();
            item5_check->setCheckState( Qt::Checked );
            ui->tableWidget->setItem( i, 0, item5_check );
        }
    }
    else {
        for ( auto it = buffer.begin(); it != buffer.end(); ++it ) {
            const int &row = it.key();
            //            qDebug() << row << it->vaild << it->key << it->value;
            QTableWidgetItem *item_check = new QTableWidgetItem();
            item_check->setCheckState( it->vaild ? Qt::Checked : Qt::Unchecked );
            ui->tableWidget->setItem( row, 0, item_check );
            ui->tableWidget->setItem( row, 1, new QTableWidgetItem( it->key ) );
            ui->tableWidget->setItem( row, 2, new QTableWidgetItem( it->value ) );
        }
    }

    foreach ( const auto &it, dev_map.values() ) {
        ui->comboBox->addItem( it );
    }
    //    foreach ( QString serialName, serialList ) {
    //        ui->comboBox->addItem( serialName );
    //    }
}

Widget::~Widget()
{
    for ( int i = 0; i < ui->comboBox->count(); ++i ) {
        QVariant var = ui->comboBox->itemData( i );
        //获取
        SerialThread *serialport = (SerialThread *)var.value<void *>();
        if ( serialport ) {
            serialport->close();
            delete serialport;
        }
    }

    DataBuffer::instance().quit();
    DataBuffer::instance().wait();

    delete ui;
}

void Widget::on_comboBox_currentIndexChanged( int index )
{
    QVariant var = ui->comboBox->itemData( index );
    //获取
    SerialThread *serialport = (SerialThread *)var.value<void *>();
    if ( !serialport ) {
        serialport = new SerialThread( ui->comboBox->itemText( index ) );
        //保存
        QVariant var = QVariant::fromValue( (void *)serialport );
        ui->comboBox->setItemData( index, var );
    }
    ui->pushButton->setText( serialport->isOpen() ? "断开" : "连接" );
}

void Widget::on_pushButton_clicked()
{
    QVariant var = ui->comboBox->currentData();
    //获取
    SerialThread *serialport = (SerialThread *)var.value<void *>();
    if ( !serialport ) {
        serialport = new SerialThread( ui->comboBox->currentText() );
        //保存
        QVariant var = QVariant::fromValue( (void *)serialport );
        ui->comboBox->setItemData( ui->comboBox->currentIndex(), var );
    }

    if ( serialport->isOpen() ) {
        serialport->close();
        ui->pushButton->setText( "连接" );
    }
    else {
        serialport->open();
        ui->pushButton->setText( "断开" );
    }
}

void Widget::onSerialReadyRead()
{
    QSerialPort *serialport = (QSerialPort *)sender();
    QByteArray   recvdata   = serialport->readAll();
    if ( !recvdata.isEmpty() ) {
        const int &rowCount = ui->tableWidget->rowCount();
        for ( int i = 0; i < rowCount; i++ ) {
            if ( ui->tableWidget->item( i, 0 )->checkState() == Qt::Checked ) {
                auto item1 = ui->tableWidget->item( i, 1 );
                auto item2 = ui->tableWidget->item( i, 2 );
                if ( !item1 || !item2 )
                    continue;

                const QString &text = item1->text();
                qDebug() << "text:" << text << text.toLatin1();
                if ( !recvdata.compare( text.toLatin1() ) ) {
                    qDebug() << "send:" << item2->text();
                    break;
                }
            }
        }
    }
    else {
    }
}

void Widget::on_tableWidget_itemChanged( QTableWidgetItem *item )
{
    const int &id = item->row();
    switch ( item->column() ) {
        case 0:
            DataBuffer::instance().setDataVaild( id, item->checkState() == Qt::Checked ? true : false );
            break;
        case 1:
            DataBuffer::instance().setDataKey( id, item->text() );
            break;
        case 2:
            DataBuffer::instance().setDataValue( id, item->text() );
            break;
    }
}
